<?php
/**
 * 定义一组h:*控件
 * 
 * <h:uri/>定义一个URI字符串
 * - * <string|array> 放在URI中的参数
 * - _except <string|array> 剔除的参数
 * - _only <string|array> 仅限的参数
 * - _extend <boolean> 是否自动继承浏览器参数，默认为true
 * - controller <string> 控制器名
 * - action <string> 动作名
 * 
 * <h:url/>定义一个URL字符串，和<h:uri/>的唯一区别是_extend属性默认为false，v1.1.2加入
 * - * <string|array> 放在URI中的参数
 * - _except <string|array> 剔除的参数
 * - _only <string|array> 仅限的参数
 * - _extend <boolean> 是否自动继承浏览器参数，默认为false
 * - controller <string> 控制器名
 * - action <string> 动作名
 * 
 * <h:link/>定义一个链接
 * - param.* <string|array> 放在URI中的参数
 * - uri.except <string|array> 剔除的参数
 * - uri.only <string|array> 仅限的参数
 * - uri.extend <boolean> 是否自动继承浏览器参数，默认为true
 * - controller <string> 控制器名
 * - action <string> 动作名
 * - uri <string> URI，指定了此参数就无需使用controller/action，v1.1.2加入
 * 
 * <h:content id="内容ID"/> 定义内容块，以便在布局中使用
 * - * <string|array> 可以在布局模板中使用的变量
 * 
 * <h:placeholder content.id="内容ID"/> 在布局模板中定义占位，好将相应的内容显示出来
 * - *content.id <string> 内容ID
 * 
 * <h:layout tpl="布局模板路径"/> 指定布局模板
 * - *tpl <string> 布局模板路径
 *
 * @since 1.1.0
 * @package if
 * @subpackage module.ui
 */
class UIHtmlControl extends UIControl {
	function render() {
		return $this->renderByTagName();
	}
	
	protected function _renderUri() {
		$attrs = $this->attrs();
		unset($attrs["_except"], $attrs["_only"], $attrs["_extend"], $attrs["controller"], $attrs["action"]);
		$uri = $this->_uri($attrs, $this->attr("_except"), $this->attr("_only"), $this->can("_extend"));
		if (!$this->attr("action")) {
			$this->setAttr("action", IController::defaultAction());
		}
		return $this->controller->path($this->attr("controller"), $this->attr("action")) . ($uri ? "?{$uri}" : "");
	}
	
	protected function _renderUrl () {
		if (!$this->hasAttr("_extend")) {
			$this->setAttr("_extend", false);
		}
		return $this->_renderUri();
	}
	
	protected function _renderLink() {
		$uri = $this->attr("uri");
		if (is_string($uri)) {
			$this->addReserveAttrs("uri,label");
			$this->setAttr("href", $uri);
		}
		else {
			$this->addReserveAttrs("controller,action,uri,label,param");
			$uri = $this->_uri($this->attr("param"), $this->attr("uri.except"), $this->attr("uri.only"), $this->can("uri.extend"));
			if (!$this->attr("action")) {
				$this->setAttr("action", IController::defaultAction());
			}		
			$uri = $this->controller->path($this->attr("controller"), $this->attr("action")) . ($uri ? "?{$uri}" : "");
			$this->setAttr("href", $uri);
		}
		if (!$this->attr("label")) {
			$this->setAttr("label", $this->content());
		}
		return "<a" . $this->renderAttrs() . ">" . $this->attr("label") . "</a>";
	}
	
	protected function _renderContent() {
		$layout = if_smarty_get_var($this->smarty, "layout");
		foreach ($this->attrs() as $name => $value) {
			$this->smarty->assign($name, $value);
		}
		if ($layout && is_file($layout)) {
			if_smarty_set_var($this->smarty, "contents." . $this->id(), $this->content());
		}
		else {
			return $this->content();
		}
	}
	
	protected function _renderPlaceholder() {
		return if_smarty_get_var($this->smarty, "contents." . $this->attr("content.id")) . "\n";
	}
	
	protected function _renderLayout() {
		$tpl = $this->smarty->template_dir . "/" . $this->attr("tpl") . o("@view.smarty.ext");
		if_smarty_set_var($this->smarty, "layout", $tpl);
	}
	
	private function _uri($params, $except, $only, $extend = false) {
		$args = array();
		if ($extend) {
			$args = IRequest::singleton()->gets();
		}
		if (is_array($params)) {
			$args = array_merge($args, $params);
		}
		$_args = array();
		foreach ($args as $name => $value) {
			if (($except && if_in_array($name, $except)) || ($only && !if_in_array($name, $only))) {
				continue;
			}
			if (is_array($value)) {
				foreach ($value as $_name => $_value) {
					$_args[] = $name . "[" . $_name . "]=" . $this->escape($_value, "url");
				}
			}
			else {
				$_args[] = $name . "=" . $this->escape($value, "url");
			}
		}
		return implode("&", $_args);
	}
	
/**
	 * 加载一个库或者一个资源
	 *
	 * 支持
	 * - lib <string> 库的名字
	 * - src <string> 资源路径
	 * 
	 * @since 1.1.2
	 */
	protected function _renderLoader() {
		//导入lib
		$lib = $this->attr("lib");
		if ($lib) {
			$this->addScriptLib($lib);
			return;
		}
		
		//src
		$src = $this->attr("src");
		$ret = array();
		if ($src) {
			$src = preg_split("/\s*,\s*/", $src);
			foreach ($src as $file) {
				$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
				if ($ext == "js") {
					$ret[] = "<script type=\"text/javascript\" src=\"{$file}\"></script>";
				}
				elseif ($ext == "css") {
					$ret[] = "<link rel=\"stylesheet\" href=\"{$file}\" type=\"text/css\" media=\"screen\" />";
				}
				else {
					$ret[] = "<script type=\"text/javascript\" src=\"{$file}\"></script>";
				}
			}
		}
		return implode("\n", $ret) . "\n";
	}	
	
	/**
	 * 定义一个<h:javascript/>控件
	 * 
	 * 该控件将一段Javascript放置在 <j:placeholder/> 控件中
	 * 
	 * <code>
	 * <head>
	 * <j:placeholder/>
	 * </head>
	 * <body>
	 * 
	 * <h:javascript>
	 * 	function alert_env() {
	 * 		alert("hello,world");
	 *		alert("env: " + "{{$smarty.const.IF_APP_ENV}}");
	 * }
	 * </h:javascript>
	 * </body>
	 * </code>
	 *
	 * @since 1.1.2
	 */
	protected function _renderJavascript() {
		$this->addScript($this->content());
	}
}

?>